/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-6-11下午07:10:11
 */
package hit.edance.campusmobile.chat.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.ReportedData.Row;
import org.jivesoftware.smackx.search.UserSearchManager;

import hit.edance.campusmobile.chat.R;
import hit.edance.campusmobile.chat.model.ActivityKiller;
import hit.edance.campusmobile.chat.service.CampusMobileService;
import hit.edance.campusmobile.chat.util.ConstantUtil;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**   
 * 搜索好友界面
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.activity
 * @Class ActivitySearchFriends
 * @Date 2012-6-11 下午07:10:11
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class ActivitySearchFriends extends Activity {

	private EditText searchEdit;
	private ListView searchResultList;
	private List<Map<String,String>> list;
	private Handler myHandler;
	private SimpleAdapter adapter;
	
	/* @see android.app.Activity#onCreate(android.os.Bundle)
	 *onCreate
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		ActivityKiller.addActivity(hashCode(), this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_searchfriends);
		myHandler = new Handler();
		initActivity();
		
	}

	/**
	 * 
	 */
	private void initActivity() {
		searchEdit = (EditText)findViewById(R.id.searchfriends);
		searchResultList = (ListView)findViewById(R.id.searchfriendsresult);
		list = new ArrayList<Map<String,String>>();
		adapter = new SimpleAdapter(this, list, R.layout.chat_searchfriends_item, new String[]{ "jabberId" }, new int[]{R.id.friendname});
		searchResultList.setAdapter(adapter);
		searchEdit.setOnTouchListener(search_OnTouch);
	}

	private void searchFriends() {
		UserSearchManager search = new UserSearchManager(
				CampusMobileService.connection);
		Form searchForm;
		try {
			searchForm = search.getSearchForm("search."
					+ CampusMobileService.connection.getServiceName());

			Form answerForm = searchForm.createAnswerForm();
			answerForm.setAnswer("Username", true);
			answerForm.setAnswer("search", searchEdit.getText().toString());
			ReportedData data = search.getSearchResults(answerForm, "search."
					+ CampusMobileService.connection.getServiceName());
			list.clear();
			Iterator<Row> it = data.getRows();
			Row row = null;
			while (it.hasNext()) {
				row = it.next();
				Map<String, String> map = new HashMap<String, String>();
				map.put("jabberId", row.getValues("jid").next().toString());
				list.add(map);
			}
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private OnTouchListener search_OnTouch = new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {
			 switch (event.getAction()) {
	            case MotionEvent.ACTION_UP:
	                int curX = (int) event.getX();
	                if (curX > ConstantUtil.widthPixels-38) {
	                	searchFriends();
	                	myHandler.post(new Runnable(){

							public void run() {
								adapter.notifyDataSetChanged();
							}
	                		
	                	});
	                    return true;
	                }
	                break;
	            }
	            return false;
		}
       
    };

}
